package demo;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import org.junit.jupiter.api.Test;

/**
 * @Classname PdfReadText
 * @Version 1.0.0
 * @Date 2022/2/24 16:07
 * @Created by yd
 */
public class PdfReadText {
    String pdfPath = "d:/金田铜业.pdf";

    @Test
    public void readText() {
        //创建PdfDocument实例
        PdfDocument doc = new PdfDocument();
        //加载PDF文件
        doc.loadFromFile(pdfPath);
        //创建StringBuilder实例
        StringBuilder sb = new StringBuilder();
        PdfPageBase page;
        //遍历PDF页面，获取每个页面的文本并添加到StringBuilder对象
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            page = doc.getPages().get(i);
            sb.append(page.extractText(true));
        }

        System.out.println(sb);
    }
}
